import { Controller, Post, Inject, Files, createMiddleware } from '@midwayjs/core';
import { UploadFileInfo, UploadMiddleware } from '@midwayjs/busboy';
import { MinioService } from '../utils/minio/minio';

@Controller('/api/file')
export class FileController {
  @Inject()
  minioService: MinioService;

  @Post('/upload', { middleware: [createMiddleware(UploadMiddleware, { mode: 'file' })] })
  async upload(@Files() files: UploadFileInfo[]) {
    try {
      // 关键功能：接收并校验上传文件（最基本校验）
      if (!files || !files.length) {
        return { code: 400, message: '未选择文件', data: null };
      }

      // 关键功能：调用 MinIO 上传并返回访问 URL
      const url = await this.minioService.minioUpload(files);
      return { url };
    } catch (error) {
      return { code: 400, message: error.message || '上传失败', data: null };
    }
  }
}